/*
 * GoodsDetailValueTitleBar.java
 * business
 *
 * Created by ChenTao on 2017/2/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yidiandao.business.R;

/**
 * 商品详情 商品评价 标题栏
 */
public class GoodsDetailValueTitleBar extends LinearLayout implements View.OnClickListener {

    private LinearLayout mLlValueAll;
    private TextView mTvValueAll;
    private TextView mTvAllNum;
    private LinearLayout mLlValueGood;
    private TextView mTvValueGood;
    private TextView mTvGoodNum;
    private LinearLayout mLlValueMid;
    private TextView mTvValueMid;
    private TextView mTvMidNum;
    private LinearLayout mLlValueWorse;
    private TextView mTvValueWorse;
    private TextView mTvWorseNum;
    private LinearLayout mLlValuePic;
    private TextView mTvValuePic;
    private TextView mTvPicNum;
    private OnBarItemClickListener mListener;

    public GoodsDetailValueTitleBar(Context context) {
        this(context, null);
    }

    public GoodsDetailValueTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);

        initViews(context);
    }

    private void initViews(Context context) {
        View rootView = LayoutInflater.from(context).inflate(R.layout.item_value_title, this, true);

        mLlValueAll = (LinearLayout) rootView.findViewById(R.id.ll_value_all);
        mTvValueAll = (TextView) rootView.findViewById(R.id.tv_value_all);
        mTvAllNum = (TextView) rootView.findViewById(R.id.tv_all_num);
        mLlValueGood = (LinearLayout) rootView.findViewById(R.id.ll_value_good);
        mTvValueGood = (TextView) rootView.findViewById(R.id.tv_value_good);
        mTvGoodNum = (TextView) rootView.findViewById(R.id.tv_good_num);
        mLlValueMid = (LinearLayout) rootView.findViewById(R.id.ll_value_mid);
        mTvValueMid = (TextView) rootView.findViewById(R.id.tv_value_mid);
        mTvMidNum = (TextView) rootView.findViewById(R.id.tv_mid_num);
        mLlValueWorse = (LinearLayout) rootView.findViewById(R.id.ll_value_worse);
        mTvValueWorse = (TextView) rootView.findViewById(R.id.tv_value_worse);
        mTvWorseNum = (TextView) rootView.findViewById(R.id.tv_worse_num);
        mLlValuePic = (LinearLayout) rootView.findViewById(R.id.ll_value_pic);
        mTvValuePic = (TextView) rootView.findViewById(R.id.tv_value_pic);
        mTvPicNum = (TextView) rootView.findViewById(R.id.tv_pic_num);

        mLlValueAll.setOnClickListener(this);
        mLlValueGood.setOnClickListener(this);
        mLlValueMid.setOnClickListener(this);
        mLlValueWorse.setOnClickListener(this);
        mLlValuePic.setOnClickListener(this);

        // 默认选择首页
        setSelected(POSITION.VALUE_ALL);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_value_all:
                if (mListener != null) {
                    mListener.onBarItemClick(POSITION.VALUE_ALL);
                    setSelected(POSITION.VALUE_ALL);
                }
                break;
            case R.id.ll_value_good:
                if (mListener != null) {
                    mListener.onBarItemClick(POSITION.VALUE_GOOD);
                    setSelected(POSITION.VALUE_GOOD);
                }
                break;
            case R.id.ll_value_mid:
                if (mListener != null) {
                    mListener.onBarItemClick(POSITION.VALUE_MID);
                    setSelected(POSITION.VALUE_MID);
                }
                break;
            case R.id.ll_value_worse:
                if (mListener != null) {
                    mListener.onBarItemClick(POSITION.VALUE_WORSE);
                    setSelected(POSITION.VALUE_WORSE);
                }
                break;
            case R.id.ll_value_pic:
                if (mListener != null) {
                    mListener.onBarItemClick(POSITION.VALUE_PIC);
                    setSelected(POSITION.VALUE_PIC);
                }
                break;
        }
    }

    public void setSelected(int position) {
        // 全部
        mTvAllNum.setEnabled(position == POSITION.VALUE_ALL);
        mTvValueAll.setEnabled(position == POSITION.VALUE_ALL);

        // 好评
        mTvGoodNum.setEnabled(position == POSITION.VALUE_GOOD);
        mTvValueGood.setEnabled(position == POSITION.VALUE_GOOD);

        // 中评
        mTvMidNum.setEnabled(position == POSITION.VALUE_MID);
        mTvValueMid.setEnabled(position == POSITION.VALUE_MID);

        // 差评
        mTvWorseNum.setEnabled(position == POSITION.VALUE_WORSE);
        mTvValueWorse.setEnabled(position == POSITION.VALUE_WORSE);

        // 晒图
        mTvPicNum.setEnabled(position == POSITION.VALUE_PIC);
        mTvValuePic.setEnabled(position == POSITION.VALUE_PIC);
    }

    public void setOnBarItemClickListener(OnBarItemClickListener listener) {
        mListener = listener;
    }

    /**
     * @param allNum   全部数量
     * @param goodNum  好评数量
     * @param midNum   中评数量
     * @param worseNum 差评数量
     * @param picNum   晒图数量
     */
    public void setData(
            String allNum, String goodNum, String midNum, String worseNum, String picNum) {
        mTvAllNum.setText(allNum);
        mTvGoodNum.setText(goodNum);
        mTvMidNum.setText(midNum);
        mTvWorseNum.setText(worseNum);
        mTvPicNum.setText(picNum);
    }

    public interface POSITION {
        int VALUE_ALL = 0;
        int VALUE_GOOD = 1;
        int VALUE_MID = 2;
        int VALUE_WORSE = 3;
        int VALUE_PIC = 4;
    }

    /**
     * 底部栏点击的回掉.
     */
    public interface OnBarItemClickListener {

        /**
         * BottomBar Item点击的回掉
         *
         * @param POSITION
         */
        void onBarItemClick(int POSITION);
    }
}
